package KK.Tip
{
	import KK.Cache.CacheManager;
	import KK.Controls.Tips.ExTip;
	import KK.Data.GameData;
	import KK.Manager.IManager;
	import KK.System.Pool.Pool;
	import KK.Time.S_Timer;
	import KK.Time.Time;
	import KK.net.ResourceManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import mx.core.Singleton;
	
	public class GlobalTip extends ExTip implements IManager
	{
		private static var timer:S_Timer;
		static private var instance:GlobalTip;
		
		
		
		private var follow:Boolean;
		private var _buttonTip:Dictionary;
		public function GlobalTip(singleton:Singleton)
		{
			//TODO: implement function 
			super();
			if(instance){
				LogError("this singleton class,pless using Instance get instance.");
			}
			instance=this;
			init();
		}
		
		/**
		 * 初始化  
		 * 
		 */
		private function init():void{
//			ResourceManager.AddSwfLoader("assets/tip.png",onLoaded);
			_buttonTip=new Dictionary();
			follow=false;
		}
		
		/**
		 * 显示消息 
		 * @param msg
		 * 
		 */
		override public function ShowMsg(msg:String):void{
			
			super.ShowMsg(msg);
			Show();
			follow=true;
		}
		
		/**
		 * 按钮tip 
		 * @param mc
		 * @param msg
		 * @param x
		 * @param y
		 * 
		 */
		public function ButtonTip(button:DisplayObjectContainer,msg:String):void{
			var btnTip:Object={"button":button,"msg":msg};
//			Sprite(button).mouseChildren=false;
			button.addEventListener(MouseEvent.MOUSE_OVER,onButtonTip);
			button.addEventListener(MouseEvent.MOUSE_OUT,onButtonTip);
			_buttonTip[button]=btnTip;
		}
		
		
		/**
		 * 鼠标移动事件  
		 * @param event
		 * 
		 */
		private function onButtonTip(event:Event):void{
			var btnTip:Object=_buttonTip[event.currentTarget];
			switch(event.type){
				case MouseEvent.MOUSE_OVER:
					follow=false;
					if(stage){
						x=stage.mouseX;
						y=stage.mouseY;
					}
					super.ShowMsg(btnTip.msg);
					Show();
					break;
				case MouseEvent.MOUSE_OUT:
					Hide();
					follow=true;
					break;
			}
			
			
		}
		
		/**
		 * 移除按钮tip 
		 * @param button
		 * 
		 */
		public function removeButtonTip(button:DisplayObject):void{
			delete _buttonTip[button];
		}
		
		
		/**
		 * 显示 
		 * 
		 */
		override public function Show():void{
			if(follow){
				if(timer&&timer.PoolStatus==Pool.USING){
					timer.setTimer(Hide,3000);
				}else{
					
					timer=Time.setTimeout(Hide,3000);
				}
			}
			super.Show();
		}
		
		
		/**
		 * 运行 
		 * 
		 */
		public function Run():void{
			initTipEvent();
			Skin=CacheManager.GetCache("tip").bitmapData;
			//GlobalTip.Instance.ShowMsg("fawefawfwaefew");
		}
		
		
		/**
		 * 
		 * 
		 */
		private function initTipEvent():void{
			GameData.Word.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
		}
		
		/**
		 * 鼠标移动  
		 * @param event
		 * 
		 */
		private function onMove(event:MouseEvent):void{
			if(visible&&follow){
				x=stage.mouseX;
				y=stage.mouseY;
			}
		}
		/**
		 * 停止  
		 * 
		 */
		public function Stop():void{
			
		}
		
		/**
		 * 加载完毕  
		 * @param event
		 * 
		 */
		private function onLoaded(event:Event):void{
			Skin=event.target.content.bitmapData;
		}
		
		
		
		/**
		 * 获取单例  
		 * @return 
		 * 
		 */
		static public function get Instance():GlobalTip{
			if(!instance){
				new GlobalTip(new Singleton);
			}
			return instance;
		}
		
	}
}


class Singleton{
	
}